package com.yango.staService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yango.commonutil.result.Result;
import com.yango.staService.client.CourseClient;
import com.yango.staService.client.MemberClient;
import com.yango.staService.entity.StatisticsDaily;
import com.yango.staService.mapper.StatisticsDailyMapper;
import com.yango.staService.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author LiDingYin
 * @since 2021-08-05
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private MemberClient memberClient;

    @Autowired
    private CourseClient courseClient;

    //生成统计数据
    @Override
    public boolean createStatisticsByDay(String day) {
        //删除已有统计数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);
        //获取统计数据
        Integer registerCount = (Integer) memberClient.getRegisterCount(day).getData().get("registerCount");//注册人数
        Integer loginCount = (Integer) memberClient.getLoginCount(day).getData().get("loginCount");//登录人数
        Integer saveCourseCount = (Integer) courseClient.getSaveCourseCount(day).getData().get("courseCount");//新增课程数
        //生成统计数据
        StatisticsDaily daily = new StatisticsDaily();
        daily.setDateCalculated(day);//统计日期
        daily.setRegisterNum(registerCount);//注册人数
        daily.setLoginNum(loginCount);//登录人数
        daily.setCourseNum(saveCourseCount);//新增课程数
        //调用方法添加
        Integer insert = baseMapper.insert(daily);
        //返回
        return insert != null && insert > 0;
    }

    //查询统计信息-图表显示
    @Override
    public Map<String, Object> showChart(String type, String begin, String end) {
        //根据统计时间查询
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.select(type, "date_calculated");//根据类型、统计时间查询
        wrapper.between("date_calculated", begin, end);//查询值在begin和end之间的数据
        List<StatisticsDaily> dailyList = baseMapper.selectList(wrapper);//查询
        //最终返回的map集合
        Map<String, Object> map = new HashMap<>();
        //返回统计数据
        List<Integer> dataList = new ArrayList<>();
        //返回统计日期
        List<String> dateList = new ArrayList<>();
        //封装进map
        map.put("dataList", dataList);
        map.put("dateList", dateList);
        //遍历封装数据
        for (StatisticsDaily daily : dailyList) {
            //封装日期
            dateList.add(daily.getDateCalculated());
            //判断统计类型，封装数据
            switch (type) {
                case "login_num"://统计登录人数
                    dataList.add(daily.getLoginNum());
                    break;
                case "register_num"://统计注册人数
                    dataList.add(daily.getRegisterNum());
                    break;
                case "course_num"://统计新增课程数
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        //返回数据
        return map;
    }
}
